import { Maze, Room, Direction, Wall, Door } from '../common'
abstract class MazeBuilder {
    builderMaze() { }
    builderRoom(room: number) { }
    builderDoor(roomFrom: number, roomTo: number) { }
    getMaze(): Maze { return new Maze() }
}

class StandardMazeBuilder extends MazeBuilder {
    _currentMaze: Maze
    constructor() {
        super()
        this._currentMaze = new Maze
    }
    commonWall(room1: Room, room2: Room): Direction {

        //需要更改
        return Direction.East
    }
    builderMaze(): void {
        this._currentMaze = new Maze()
    }
    builderRoom(roomNo: number): void {
        if (!this._currentMaze.getRoom(roomNo)) {
            console.log('房间不存在，开始创建。。。。。。。。。');
            const room = new Room(roomNo)
            this._currentMaze.addRoom(room)

            room.setSide(Direction.North, new Wall())
            room.setSide(Direction.South, new Wall())
            room.setSide(Direction.East, new Wall())
            room.setSide(Direction.West, new Wall())
        }

    }
    builderDoor(roomFrom: number, roomTo: number): void {

        const r1 = this._currentMaze.getRoom(roomFrom)
        const r2 = this._currentMaze.getRoom(1)
        const d = new Door(r1, r2)
        r1.setSide(this.commonWall(r1, r2), d)
        r2.setSide(this.commonWall(r2, r1), d)
    }
    getMaze(): Maze {
        return this._currentMaze
    }
}

class CountingMazeBuilder extends MazeBuilder {
    declare private _doors: number
    declare private _rooms: number

    countingMazeBuilder() {
        this._doors = this._rooms = 0
    }
    builderRoom(room: number): void {
        this._rooms++
    }
    builderDoor(roomFrom: number, roomTo: number): void {
        this._doors++
    }
    getDoorsCounts() {
        return this._doors
    }
    getRoomsCounts() {
        return this._rooms
    }
}
class MazeGameBuilder {
    createMaze(builder: MazeBuilder): Maze {
        builder.builderMaze()
        builder.builderRoom(0)
        builder.builderRoom(1)

        builder.builderDoor(0, 1)

        return builder.getMaze()
    }
    CreateComplexMaze(builder: MazeBuilder): Maze {
        return builder.getMaze()
    }
}

//客户端
class Client {
    static main() {
        // let maze: Maze = new Maze()
        const game: MazeGameBuilder = new MazeGameBuilder()
        const builder: CountingMazeBuilder = new CountingMazeBuilder()

        game.createMaze(builder)
        builder.getDoorsCounts()
        const maze = builder.getMaze()
        console.log(maze);
    }
}

Client.main()

